package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.qiniu.SevenUploadUtil;
import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IHotelService;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("hotel")
public class HotelController {
    @Autowired
    private IHotelService hotelService;
    @Autowired
    private IRegionService regionService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){
        PageInfo pageInfo = hotelService.queryForList(qo);
        model.addAttribute("result",pageInfo);
        model.addAttribute("places",regionService.selectAll());
        return "/hotel/list";
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Hotel hotel, MultipartFile file){
        JSONResult result = new JSONResult();
        try {
            if(file != null && file.getSize() > 0){
                //String imgUrl = UploadUtil.upload(file, UploadUtil.BASE_IMAGE_PAGE + "upload");
                String  imgUrl= SevenUploadUtil.uploadQiniu(file);
                hotel.setCoverUrl(imgUrl);
            }
            hotelService.saveOrUpdate(hotel);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
